function [bottle_mass_center, bottle_shape, cap_mass_center, cap_shape]=...
    bottle_model(x_points, y_points, x_points_cap, y_points_cap)

% This code generates the bottle and moves the coordinates so that one edge
% of the bottom of the bottle is in origo. It also calculates the centre of
% mass of the bottle in same coordinates, when the density of the bottle is
% homogenious. It also creates the cap and calculates the mass centre
% coordinates of it. 

% Input arguments:
% x_points are the points where the radius of the bottle was measured
% y_points are the radii of the bottle in x_points
% The origo has to be in the middle of the bottle in the top part of bottle
% x_points_cap are the x coordinates of the WHOLE cap
% y_points_cap are the y coordinates of the WHOLE cap

% Output arguments:
% bottle_mass_center is the centre of mass of the bottle in the moved coordinate
% system
% bottle_shape are the points of the bottle in the moved coordinate
% system, the last coordinate is given the same as the first one, so that
% the bottle is "closed"
% cap_mass_center is the mass centre of cap



lengths_stored=[];
middle_points=[];
% Calculate the middle points of the bottle coordinates and the length of
% the lines between points.
for ii = 1:length(x_points)-1
    
    middle_point=[(x_points(ii)+x_points(ii+1))/2;(y_points(ii)+y_points(ii+1))/2];
    length_of_lines=sqrt((x_points(ii)-x_points(ii+1))^2+(y_points(ii)-y_points(ii+1))^2);
    
    middle_points = [middle_points middle_point];
    lengths_stored = [lengths_stored length_of_lines];
end
total_length_of_edge = sum(lengths_stored);


lengths_stored_c = [];
middle_points_c = [];
% Calculate the middle points of the cap coordinates and the length of
% the lines between points.
for jj = 1:length(x_points_cap)-1
    
    middle_point_c = [(x_points_cap(jj)+x_points_cap(jj+1))/2;...
                    (y_points_cap(jj)+y_points_cap(jj+1))/2];
    length_of_lines_c = sqrt((x_points_cap(jj)-x_points_cap(jj+1))^2+...
                    (y_points_cap(jj)-y_points_cap(jj+1))^2);
    
    middle_points_c = [middle_points_c middle_point_c];
    lengths_stored_c = [lengths_stored_c length_of_lines_c];
end
total_length_of_edge_c=sum(lengths_stored_c);



mc_line_c=0;

% Calculate the mass centre of cap
for jj=1:length(middle_points_c)
    
    mc_line_c=mc_line_c+middle_points_c(:,jj)*lengths_stored_c(jj);
    
end
cm_point_c=1/total_length_of_edge_c*mc_line_c;


mc_line=0;

% Calculate the mass centre
for jj=1:length(middle_points)
    
    mc_line=mc_line+middle_points(:,jj)*lengths_stored(jj);
    
end
cm_point=1/total_length_of_edge*mc_line;
cm_point=[cm_point(1) 0];


% rotation matrix to put bottle model in vertical position
rotation = rotation_matrix(-pi/2);

%Find the rotation point
vect=find(x_points==max(x_points));
indeksi=max(find(y_points==max(y_points(vect))));
axispoint=[x_points(indeksi); y_points(indeksi)];

%Move the mass centre of bottle
tmp0 = [cm_point(1); cm_point(2)]-axispoint;
cm_point_x=tmp0(1,:);
cm_point_y=tmp0(2,:);
bottle_mass_center=rotation * [cm_point_x; cm_point_y];

%Move the mass centre of cap
tmp0_c = [cm_point_c(1); cm_point_c(2)]-axispoint;
cm_point_x_c=tmp0_c(1,:);
cm_point_y_c=tmp0_c(2,:);
cap_mass_center=rotation * [cm_point_x_c; cm_point_y_c];



pp=x_points;
y2=-y_points;
if y2(end)==y_points(end)
    y2=[y2(1:end-1)];
    pp=pp(1:end-1);
end
%Move the other edge of the bottle
tmp = [x_points; y_points]-axispoint*ones(1,size(x_points,2));


[x_points]=tmp(1,:);
[y_points]=tmp(2,:);
% Move the other edge
tmp2 = [pp; y2]-axispoint*ones(1,size(y2,2));
[x2]=tmp2(1,:);
[y2]=tmp2(2,:);

x2_rev=x2(end:-1:1);
y2_rev=y2(end:-1:1);

x_coord=[x_points x2_rev x_points(1)];
y_coord=[y_points y2_rev y_points(1)];
bottle_shape=rotation * [x_coord; y_coord];

%Move the cap
tmp3 = [x_points_cap; y_points_cap]-axispoint*ones(1,size(x_points_cap,2));
[x_coord_c]=tmp3(1,:);
[y_coord_c]=tmp3(2,:);
cap_shape=rotation * [x_coord_c; y_coord_c];

